﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class GioHang : System.Web.UI.Page
{
    static DataTable tblGioHang = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        if (Session["GioHang"] != null)
        {
            tblGioHang = Session["GioHang"] as DataTable;
            gvGioHang.DataSource = tblGioHang;
            gvGioHang.DataBind();

            string tongTien = tblGioHang.Compute("Sum(TongTien)", "").ToString();
            lblTongTien.Text = "Tổng tiền: " + tongTien + ".000 VNĐ";
        }
       
        if ((tblGioHang.Rows.Count <=0))
        {
            lblMessage.Visible = true;
            lblMessage.Text = "Không có sản phẩm nào trong giỏ hàng.";
            lblThongTin.Visible = false;
            lblTongTien.Visible = false;
            btnDatHang.Visible = false;
        }
    }
    protected void gvGioHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblId = (Label)gvGioHang.Rows[e.RowIndex].FindControl("lblIdSP");
        int IdSP = Convert.ToInt16(lblId.Text);

        DataTable dt = (DataTable)Session["GioHang"];

        foreach (DataRow row in dt.Rows)
        {
            if ((int)row["idSP"] == IdSP)
            {
                dt.Rows.Remove(row);           
                break;
            }
        }
        Session["Giohang"] = dt;
        Response.Redirect("~/GioHang.aspx");


    }
    protected void btnDatHang_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/DatHang.aspx");
    }
}